library(raster)
Find the 3 letter ISO code of a country you like.s Here is a list of codes: https://www.iban.com/country-codes
Create an object called iso. This will be 3 letter ISO code. For example, here is Tanzania (but you can do a different country)
iso <- 'TZA'
x. Set the level to 0 if you want for only the country, set it higher if you want more detailedx <- getData(country = iso, level = 2)
plot(x)
library(choroplethr)
library(choroplethrMaps)
library(tidyverse)
library(gapminder)
gmgm <- gapminder
gm <- gm %>%
rename(region = country,
value = lifeExp) %>%
mutate(region = tolower(region))
gm_choro <- gm %>% filter(year == 2007)
country_choropleth(gm_choro)
country_choropleth(gm_choro, num_colors = 4)
library(tidyverse)
library(rgdal)
library(raster)
library(sp)
moz0.library(raster)
library(leaflet)
moz0 <- getData(country = 'MOZ', level = 0)
leaflet() %>%
addProviderTiles("Esri.WorldImagery") %>%
addPolygons(data = moz0)
moz1 <- getData(country = 'MOZ', level = 1)
leaflet() %>%
addProviderTiles("Esri.WorldImagery") %>%
addPolygons(data = moz1)
gaza <- moz1[moz1$NAME_1 == 'Gaza',]
leaflet() %>%
addProviderTiles("Esri.WorldImagery") %>%
addPolygons(data = gaza)
us <- read_csv("https://raw.githubusercontent.com/databrew/databrew.github.io/master/us.csv")
gaza_us <- us %>% filter(province == 'GAZA')
load(url('https://github.com/databrew/maputo/raw/master/day3/mop.RData'))
## Regions defined for each Polygons
## Regions defined for each Polygons
theme_map() to remove the background## Regions defined for each Polygons
## Regions defined for each Polygons
geom_path with the mopeia_waterways object. Color the water blue.## Regions defined for each Polygons
mopeia_roads object. Color them brown## Regions defined for each Polygons
mopeia_us data and add group=NA to within your call to geom_point().## Regions defined for each Polygons
addPolylines function for roads and water.weight makes things thinneropacity controls alpha## Assuming "longitude" and "latitude" are longitude and latitude, respectively
library(tidyverse)
terra like this:terra <- read_csv('https://github.com/databrew/maputo/blob/master/day4/terra.csv?raw=true')
Plot terra
Plot terra with clear borders between each country
Create an object named mozy. This will just be Mozambique.
Plot mozy.
Plot mozy in an interactive map.
Load up the gapminder data as an object called gm.
gm <- gapminder::gapminder
Filter the gapminder data to only include data from 2007. Call this recent.
Perform a join between terra and recent so as to associate geographic data with health/wealth data. Call the new objected combinado
Plot combinado using ggplot, but filling each country as a function of gdpPerCap
Let’s alter the color scale by adding the following:
scale_fill_gradientn(colors = c('red', 'white', 'blue'))
library(leaflet)
library(sp)
library(tidyverse)
library(raster)
violence <- read_csv("https://github.com/databrew/maputo/blob/master/day3/data/moz.csv?raw=true")
moz1 <- getData(country = 'MOZ', level = 1)
In violence, create a new column named province. This will be simply the admin1 column copied.
In moz1, create a new column called province. This will be simply the NAME_1 column copied.
In moz1, change the word “Nassa” to “Niassa” in the province column.
Create an object called bp. This will be the number of fatalities in violent events by province, only since 2010.
Join moz1 and bp. NOTE, since moz1 is not a normal dataframe (it’s a “SpatialPolygonsDataFrame”), we have to specifically use it’s “data” component. The below is how to do this.
moz1@data <- left_join(x = moz1@data,
y = bp,
by = 'province')
Make an interactive map with moz1
Create a choropleth of the deaths data
pal <- colorNumeric("Spectral", domain = range(moz1@data$deaths))
leaflet() %>%
addTiles() %>%
addPolygons(data = moz1,
fillColor = ~pal(deaths),
weight = 1,
color = 'black',
opacity = 0.9)
leaflet() %>%
addTiles() %>%
addPolygons(data = moz1,
fillColor = ~pal(deaths),
weight = 1,
color = 'black',
opacity = 0.9) %>%
addLegend(pal=pal, values=moz1$deaths, opacity=0.9, title = "Deaths since 2010", position = "bottomright" )
library(tidyverse)
weather <- read_csv("https://github.com/databrew/maputo/blob/master/day3/data/mozambican_weather.csv?raw=true")
Take a look at the top of the data.
Create an object called x. This will be the weather data, then summarized to tell us the minimum and maximum dates.
Create an object called y. This will be the weather data, then summarized to tell us the maximum temp_max and the mimimum temp_min.
Create an object called z. This will be the weather data, but filtered to only include the year 2014 and only the district of “MATOLA”.
Make a plot of z with the date on the x-axis and the average temperature (temp) on the y-axis.
Make a plot of z showing the distribution of precipitation.
Make a plot showing the distribution of the maximum temperature, but faceted by district.
Color the lines in the above red.
Add another distribution the above chart with the minimum temperature. Color it blue. To do this you will need to add another geom_density line to your chart, and you can include another aes() section within that geom_density function.
library(tidyverse)
weather <- read_csv("https://github.com/databrew/maputo/blob/master/day3/data/mozambican_weather.csv?raw=true")
Take a look at the top of the data.
Create an object called pd. This will be some data we want to plot. It will include only the columns we want to plot. For example.
pd <- weather %>%
dplyr::select(district, date, temp_max, temp_min)
gatherpdl <- pd %>%
gather(key, value, temp_max:temp_min)
Make a plot of temperature with the color showing the key and paneling/faceting for each district.
Read in the gapminder data
library(gapminder)
gm <- gapminder
Create an object called x. This will only be data for countries in Oceania
Make the data “long” by gathering the lifeExp, pop, and gdpPercap variables.
x <- x %>% gather(key, value, lifeExp:gdpPercap)
Plot the x data with year on the x-axis, the value on the y-axis, colored by country, and panelled/faceted by key. Add scales = 'free_y' to within the facet_wrap function in order to have “loose” scales.
Filter x to remove population. Plot again (using the same code from 9)
library(tidyverse)
data('swiss')
swiss <- swiss
Look at the cabeça of the data. Describe it.
Make a chart showing the association between Agriculture rate and Fertility rate. This should be a scatterplot. You’ll use geom_point.
Describe the association.
Add the argument geom_smooth() to the above chart. What happens?
Run the following code to create a variable called canton from the row.names of the data:
swiss$canton <- row.names(swiss)
Create an object called x. This will be the swiss data, but only keeping those cantons which are more than 80% catholic.
How many rows are in x?
Go back to your swiss data. Is there an association between Education and Catholicism? Show it in a chart.
Create a variable a new variable in swiss called edu, which indicates whether the canton is highly-educated (at or above the average) or has low education (below the average). Here’s the code to do this:
swiss <- swiss %>%
mutate(edu = ifelse(Education >= mean(Education),
"High education",
"Low education"))
Create a variable in swiss called fert. This variable should show whether the canton has high fertility or low fertility. Use the code in the previous question as a guide.
Create an object called y. This will be the swiss data, grouped by both your new edu and fert variables, and then summarized to tell the average infant mortality rate for each subgroup.
Make a barplot of the data in y. Add the argument position = 'dodge to within geom_bar() to make the bars not stack
Interpret the above chart. Which group has the highest infant mortality? Which one has the lowest?
Arrange the data by infant mortality. Which canton has the lowest?
What is the maximum infant mortality for all Swiss canons?
What is the maximum infant mortality for canons with high education?
What is the average infant mortality for canons with high fertility.
Make a plot showing the relationship between fertility (on the x-axis) and infant mortality (on the y-axis).
Add color = edu to within the aes() section of the above chart.
Make a “density” plot of fertility.
Create a new variable in swiss using mutate. The new variable will be named ag. This should be whether a canton is very agricultural or not (ie, more than 50%). To do this:
swiss <- swiss %>%
mutate(ag = ifelse(Agriculture > 50, 'Very agricultural',
'Not very agricultural'))
Create an overlapping density chart of infant mortality in which the fill shows whether a place is very agricultural or not.
Create a new variable in your data called cath. This should whether or not a canton is very Catholic (ie, > 50%).
Create an overlapping density chart of fertility in which the fill is whether a place is very Catholic or not.
Use ggridges to create a ridgeline chart of the distribution of infant mortality by your cath variable (here’s the code to help)
library(ggridges)
ggplot(data = swiss,
aes(x = Infant.Mortality,
y = cath)) +
ggridges::geom_density_ridges()
Create a ridgeline chart (like above) showing fertility instead of infant mortality.
Which areas have more children: Catholic or non-Catholic areas?
Use the cut function to generate a 4-category categorical variable of the Education variable. Call this new variable educat. Note that cut takes two arguments: first, the variable you are cutting; second, the number of categories you want.
Make a ridgeline plot of infant mortality’s distribution by your new educat variable.
Can you create the following plot? You’ll need to add facet_wrap(~educat)…
ggplot(data = swiss,
aes(x = Catholic,
y = Infant.Mortality,
size = Fertility)) +
facet_wrap(~educat) +
geom_point(alpha = 0.6)
What percentage of high-Catholicism cantons are agricultural?
What percentage of low fertility canons are Catholic?
frangos.data('ChickWeight')
frangos <- ChickWeight
2 How many columns does the data have?
3 How many rows does the data have?
4 What are the variable types (quantitative/numeric or categorical)?
5 Create a point chart showing weight on the x-axis and time on the y-axis.
6 Create an object called frango1. This should be just the data for chicken number 1 (ie, 1 in the Chick column).
7 Chart the weight of chicken 1 over time using geom_point()
8 Chart the weight of chicken 1 over time using geom_line()
9 Chart the weight of chicken 1 over time using geom_area()
10 Chart the weight of all chickens over time using geom_line(). Make the color of each line different for each chicken.
11 Create an object called zero. This should be the frangos only at day 0 (ie, when they are born).
12 Make a histogram of chicken’s weights at day 0.
13 Make a density plot of chicken’s weights at day 0.
14 Make a violin plot of chicken’s weights at day 0. On the x-axis, put the Diet type. On the y-axis, put the weight.
15 Add points to the violin plot
knitr
rmarkdown
DT
In the upper left of RStudio, click “File”, then “New File”, then “R Markdown”.
Name your file: “Mapping with R”
In your new file, run CTRL + s in order to save. Save it as “mapping” (for example).
Delete lines 11 until the end.
Write the following in order to create a “header” section:
## How to get data
Move your cursor below that section, and then click on “Insert” at the top of the code window. From there, click on “R”.
You now have a section for writing R code. In that section, write:
library(raster)
library(leaflet)
library(tidyverse)
Click “Knit” in the top of the window in order to compile the document.
Now add more text and R sections to create a complete guide for mapping in R.